package com.ruoyi.common.utils;

import com.ruoyi.common.config.ReadFileConfig;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class PicUrlUtil {

    @Autowired
    private ReadFileConfig readFileConfig;

    public String handlePicUrl(String url) {

        if (StringUtils.isNotBlank(url)) {
            String[] urlArr = url.split(",");
            List<String> urlList = Arrays.stream(urlArr).map(item -> {
                if (item.startsWith(readFileConfig.getUrlPrefixHttp()) || item.startsWith(readFileConfig.getUrlPrefixHttps())) {
                    return item;
                } else {
                    item = item.replaceFirst("/profile", readFileConfig.getUrlPrefixHttps());
                    return item;
                }
            }).collect(Collectors.toList());
            if (urlArr.length == 1) {
                return urlList.get(0);
            } else {
                return StringUtils.join(urlList,",");
            }
        }
        return url;
    }

}
